//go:build ignore

package main

import (
	"auto/base"
	"flag"
	"fmt"
	"math/rand"
	"time"

	"gorm.io/gorm"
)

var dbRemote *gorm.DB
var dbLocal *gorm.DB
var remoteDianpus []base.RemoteDianpu

var remoteTable string
var localTable string

func init() {
	var err error
	dbRemote, err = base.DbConnectRemote()
	if err != nil {
		fmt.Println("远程数据库连接错误：", err.Error())
		base.LogWrite(fmt.Sprintf("info：远程数据库连接错误，%v", err.Error()), "release")
		return
	}
	dbLocal, err = base.DbConnectLocal()
	if err != nil {
		fmt.Println("本地数据库连接错误：", err.Error())
		base.LogWrite(fmt.Sprintf("info：远程数据库连接错误，%v", err.Error()), "release")
		return
	}
}

func main() {
	var catename string
	var keyword string
	var cityname string
	var num int
	var more int
	flag.StringVar(&remoteTable, "r-table", "", "远程数据表名")
	flag.StringVar(&keyword, "r-keyword", "", "远程检索关键词")
	flag.StringVar(&cityname, "r-city", "", "远程检索城市")
	flag.IntVar(&num, "r-num", 0, "远程检索数量")
	flag.IntVar(&more, "r-more", 0, "远程筛选切换")
	flag.StringVar(&localTable, "l-table", "", "本地数据表名")
	flag.StringVar(&catename, "l-cate", "", "本地栏目名称")
	flag.Parse()
	if remoteTable == "" || localTable == "" || catename == "" {
		fmt.Println("启动参数错误")
		return
	}
	cate_id := getCateid(catename)
	if cate_id == 0 {
		fmt.Println("栏目获取失败")
		base.LogWrite("栏目获取失败", "release")
		return
	}
	limit := 100
	count := 0
	for {
		if num != 0 && count >= num {
			fmt.Println("执行完毕")
			base.LogWrite("执行完毕", "release")
			return
		}
		tx := dbRemote.Table(remoteTable)
		tx.Where("is_use = 0")
		if more == 0 {
			tx.Where("!LOCATE('|',type)")
		}
		if keyword != "" {
			tx.Where("name LIKE ?", "%"+keyword+"%")
		}
		if cityname != "" {
			tx.Where("cityname = ?", cityname)
		}
		tx.Limit(limit).Find(&remoteDianpus)
		if len(remoteDianpus) == 0 {
			fmt.Println("没有数据")
			base.LogWrite("没有数据", "release")
			return
		}
		for _, v := range remoteDianpus {
			insertLocal(&v, cate_id)
		}
		count += limit
	}
}

func insertLocal(remoteDianpu *base.RemoteDianpu, cate_id int32) {
	dianpu := &base.LocalDianpu{}
	areaid1, areaid2, areaid3 := getAreaids(remoteDianpu.Pname, remoteDianpu.Cityname, remoteDianpu.Adname)

	dianpu.Cate_id = cate_id
	dianpu.Areaid1 = areaid1
	dianpu.Areaid2 = areaid2
	dianpu.Areaid3 = areaid3
	dianpu.Hits = rand.Intn(500)
	dianpu.Create_time = time.Now().Unix()
	dianpu.Update_time = time.Now().Unix()
	dianpu.Uid = remoteDianpu.Uid
	dianpu.Name = remoteDianpu.Name
	dianpu.Location = remoteDianpu.Location
	dianpu.Pname = remoteDianpu.Pname
	dianpu.Cityname = remoteDianpu.Cityname
	dianpu.Adname = remoteDianpu.Adname
	dianpu.Pcode = remoteDianpu.Pcode
	dianpu.Citycode = remoteDianpu.Citycode
	dianpu.Adcode = remoteDianpu.Adcode
	dianpu.Address = remoteDianpu.Address
	dianpu.Business_area = remoteDianpu.Business_area
	dianpu.Type = remoteDianpu.Type
	dianpu.Tag = remoteDianpu.Tag
	dianpu.Tel = remoteDianpu.Tel
	dianpu.Cost = remoteDianpu.Cost
	dianpu.Rating = remoteDianpu.Rating
	dianpu.Photos = remoteDianpu.Photos
	if err := dbLocal.Table(localTable).Create(dianpu).Error; err != nil {
		fmt.Println("执行失败")
		base.LogWrite(fmt.Sprintf("模型：%v，城市：%v，名称：%v，UID：%v，info：数据插入失败！", localTable, dianpu.Cityname, dianpu.Name, dianpu.Uid), "release")
		return
	}
	fmt.Println("执行成功")
	dbRemote.Table(remoteTable).Where("uid = ?", remoteDianpu.Uid).Update("is_use", 1)
	base.LogWrite(fmt.Sprintf("模型：%v，城市：%v，名称：%v，UID：%v，info：执行成功！", localTable, dianpu.Cityname, dianpu.Name, dianpu.Uid), "release")
}

func getAreaids(name1, name2, name3 string) (int32, int32, int32) {
	area1 := struct {
		Id int32
	}{}
	area2 := struct {
		Id int32
	}{}
	area3 := struct {
		Id int32
	}{}
	dbLocal.Table("jrk_area").Where("pid = ? AND instr(?,name)", 0, name1).Scan(&area1)
	if area1.Id != 0 {
		dbLocal.Table("jrk_area").Where("pid = ? AND instr(?,name)", area1.Id, name2).Scan(&area2)
		if area2.Id != 0 {
			dbLocal.Table("jrk_area").Where("pid = ? AND instr(?,name)", area2.Id, name3).Scan(&area3)
		}
	}
	return area1.Id, area2.Id, area3.Id
}

func getCateid(name string) int32 {
	result := struct {
		Id int32
	}{}
	dbLocal.Table("jrk_articles_cate").Where("name = ?", name).Scan(&result)
	return result.Id
}
